package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfBusiness;
import com.yuandian.api.management.entity.ConfChannelMapping;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfChannelMappingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.Wrapper;
import java.util.Collection;
import java.util.List;

/**
 * -Controller
 * @author zfsn
 * @date 2023-5-22
 */
@Api(tags = "渠道配置")
@RestController
@RequestMapping("/channelMapping")
public class ConfChannelMappingController {

	@Autowired
	private ConfChannelMappingService confChannelMappingService;

	/**
	 * 通过主键查询数据
	 * @param id 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true, dataType = "Integer")
	})
	public R<ConfChannelMapping> queryByIds(@PathVariable("id") Integer id) {
		return R.ok(confChannelMappingService.getById(id));
	}

	/**
	 * 分页查询
	 * @param confChannelMapping 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "channelCode", value = "渠道编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "channelName", value = "渠道名称", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataType = "Integer"),
	})
	public R<Page<ConfChannelMapping>> listPage(@RequestBody @ApiIgnore ConfChannelMapping confChannelMapping) {
		return R.ok(confChannelMappingService.listPage(confChannelMapping));
	}

	/**
	 * 新增数据
	 * @param confChannelMapping 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "channelCode", value = "渠道编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "channelName", value = "渠道名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer")
	})
	public R<Boolean> save(@Validated({InsertFlag.class}) @RequestBody @ApiIgnore ConfChannelMapping confChannelMapping) {
		QueryWrapper<ConfChannelMapping> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("channel_code", confChannelMapping.getChannelCode());
		List<ConfChannelMapping> confChannelMappings = confChannelMapping.selectList(queryWrapper);
		if (!CollectionUtils.isEmpty(confChannelMappings)) {
			return R.failed("同名渠道编码已经存在！");
		}

		return R.ok(confChannelMappingService.save(confChannelMapping));
	}

	/**
	 * 通过主键更新数据
	 * @param confChannelMapping 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@SysLog("更新数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "channelCode", value = "渠道编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "channelName", value = "渠道名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer")
	})
	public R<Boolean> updateById(@Validated({UpdateFlag.class}) @RequestBody @ApiIgnore ConfChannelMapping confChannelMapping) {
		QueryWrapper<ConfChannelMapping> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("id", confChannelMapping.getId());
		queryWrapper.eq("channel_code", confChannelMapping.getChannelCode());
		List<ConfChannelMapping> confChannelMappings = confChannelMapping.selectList(queryWrapper);
		if (!CollectionUtils.isEmpty(confChannelMappings)) {
			return R.failed("同名渠道编码已经存在！");
		}

		return R.ok(confChannelMappingService.updateById(confChannelMapping));
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@DeleteMapping("/delete/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataType = "Integer"),
	})
	public R<Boolean> removeById(@PathVariable("ids") @ApiIgnore List<Integer> ids){
		return R.ok(confChannelMappingService.removeByIds(ids));
	}
}